import requests,time,xlrd
from tinyshop.COMMON.public_Back_login import back_login

class promotion:
    def __init__(self):
        self.s = requests.session()
    def all(self):
        # 后端登录传入报告的结果，列表格式，待driver中与其他模块整合
        # self.list_back_End_Login = []
        # print(self.list_back_End_Login)
        # 15为用例总条数
        for i in range(1, 10):
            self.back_promotion_goods(i)
            # self.list_back_End_Login.append(self.tup)
            time.sleep(1)
        # return self.list_back_End_Login

    # 后端登录接口测试
    #     1.后端输入账户密码正确验证码为空，登录成功（验证码前端显示为“aaaa”）
    def back_promotion_goods(self,i):
        book = xlrd.open_workbook(r'C:\Users\zhangqi\PycharmProjects\TinyShop_v17_Interface_testing\Data\Test_Data.xlsx')
        s = book.sheets()[2]
        li = s.row_values(i)

        # 传参的步骤
        sp = self.s.post("http://192.168.34.146/TinyShop_v1.7/index.php?con=marketing&act=prom_goods_save", data={
            "name": li[6], "type":li[7], "expression": li[8], "start_time":li[9],"end_time":li[10],"group[]":li[11],"goods_id[]":li[12],"description":li[13]
        })
        ui = sp.text
        if li[6] in ui:
            print("商品促销添加成功！！")
            self.result = "促销添加成功"
        else:
            print("商品促销添加失败！！")
            self.result = "促销添加失败"
        # 为报表最终测试结果做判定，以正确输出
        if li[5] == self.result:
            self.final_re = "用例通过"
        else:
            self.final_re = "用例未通过"
        # 生成结果元祖
        # self.tup = {"t_id":li[0],"t_mod":li[1],"t_obj":li[2],"t_mtd":li[3],"t_param":li[4],"t_hope":li[5],
        #        "t_actual":self.result,"t_result":self.final_re}
        file = r"C:\Users\zhangqi\PycharmProjects\TinyShop_v17_Interface_testing\Data\interface_test_report.txt"
        with open(file,"a+",encoding="utf-8") as f:
            f.write("%s,%s,%s,%s,%s,%s,%s\r"%(li[0],li[1],li[2],li[3],li[5],self.result,self.final_re))

# login().all()
